package com.study.webapp.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class WriteToRead {

	ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

	public void CachedData() {
		lock.readLock().lock();
		// 做某些操作
		System.out.println(Thread.currentThread().getName());
		// 获取写锁前一定要先释放读锁
		lock.readLock().unlock();
		lock.writeLock().lock();
		System.out.println(Thread.currentThread().getName());
		// 在释放写锁前可以获取读锁
		lock.readLock().lock();
		lock.writeLock().unlock();
		// 释放完写锁再释放读锁
		lock.readLock().unlock();
	}

}
